/**
 * <copyright> 
 *
 * Copyright (c) 2008 itemis AG and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   itemis AG - Initial API and implementation
 *
 * </copyright>
 *
 */
package com.rd.emf.geneditor.view.editors.generic;

import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.impl.EStructuralFeatureImpl.ContainmentUpdatingFeatureMapEntry;
import org.eclipse.emf.edit.provider.WrapperItemProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.ui.forms.IDetailsPage;
import org.eclipse.ui.forms.IDetailsPageProvider;


/**
 * @author Dennis Huebner
 * 
 */
public class EEDetailsPageProvider implements IDetailsPageProvider {
	private GenericDetailsPage page;
	private com.rd.emf.geneditor.view.editors.generic.EMFGenericEditor editor;

	public EEDetailsPageProvider(com.rd.emf.geneditor.view.editors.generic.EMFGenericEditor editor) {
		Assert.isNotNull(editor);
		this.editor = editor;
		// domain.getCommandStack().addCommandStackListener(this);
	}

	public IDetailsPage getPage(Object key) {
		EObjectImpl input = null;
		
		if (key instanceof EObjectImpl)
		{
			EObjectImpl eModelElement = (EObjectImpl) key;
			input = eModelElement;
		}
		else if (key instanceof WrapperItemProvider)
		{
			WrapperItemProvider wip = (WrapperItemProvider) key;
			return this.getPage(wip.getValue());
		}
		else if (key instanceof ContainmentUpdatingFeatureMapEntry)
		{
			ContainmentUpdatingFeatureMapEntry cufme = (ContainmentUpdatingFeatureMapEntry) key;
			return this.getPage(cufme.getValue());
		}
		
		page = new GenericDetailsPage(input, editor);
		page.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				editor.setSelection(event.getSelection());
			}
		});
		return page;
	}

	public Object getPageKey(Object object) {
		return object;
	}

}